var START_CARDS = 5;

var TURN_START = 0;
var TURN_PLAY = 1;
var TURN_END = 2;

ExtGames.UnoGame = function(config) {
	
	this.wildToPlay = null;
	
	ExtGames.UnoGame.superclass.constructor.call(this, config);

};

Ext.extend(ExtGames.UnoGame, ExtGames.Game, {
	

	nextTurn: function() {
		ExtGames.UnoGame.superclass.nextTurn.call(this);
	},
	
 	callUno: function() {
	  if (this.turn != TURN_END) {
		  return;
	  }
	  
	  if (Ext.getCmp('unoButton').pressed == true) {
		  submitMayIRequest();
		  this.win.updateStatusCallMayI(getName(getMyId()));
	  }
    },

	getRoundCriteria: function() {	  
	  var round = {};
	  round['runs'] = 0;
	  round['groups'] = 0;
	  return round;
  	},
  	
  	isGameEnd: function() {
  		return true; 
  	},

	getNumberOfCardsToDeal: function() {
		return START_CARDS;
	},
	
	getToolbarButtons: function() {
		var buttons = [];
		buttons.push(new Ext.Button({
		    id: 'unoButton',
		    text:    'Uno',
		    enableToggle: true,
		    allowDepress: false,
		    scope:   this,
		    toggleHandler: function(button, state) {
		      game.callUno();
		    }
		}));
		
		return buttons;
	},
	
	dropCard: function(stack, card, e) {	  
	  	  
		  var from = card.location.itemId;
		  var to = stack.itemId;
		  
		  console.log('DROP CARD from '+from+' to '+to);
		  	  
		  if (from == 'hand' && to == 'hand') {
		  	  this.hands[0].rearrange(card, e);
		  	  return;
		  }
		  
		  if (this.turn == TURN_START) {
			  if (from == 'hand' && to == 'discardPile') {
				  
				  if (!this.isValidPlay(card, stack)) {
					  return;
				  }
				  
				  card.move(stack);
				  
				  if (card.joker && card.joker == true && this.hands[0].getNumCards() > 1) {
					  this.openWildSelection();
					  return;
				  }
				  
				  var s = {};
				  s.from = from;
				  s.card = card.toObject();
				  util.discard(s);
				  
				  if (this.hands[0].getNumCards() == 0) {
					  this.endRound();
				  }
				  else {
					  util.endTurn();
				  }
			  }
			  else if (from == 'unuseddeck' && to == 'hand') {	  	
				  card.reveal();
				  card.move(stack);
				  this.hands[0].sortCards();
				  
				  var s = {};
				  s.from = from;
				  s.card = card.toObject();
				  util.drawCard(s);
				  
				  this.win.updateStatus('You drew a '+card.toPlainText());
			  }
			  
			  
				  
		  }
 
	  },
	  
	  isValidPlay: function(card, discardPile) {
		  var lastCard = discardPile.getLastCard();
		  if (card.joker == true) {
			  return true;
		  }
		  if (lastCard.joker == true) {
			  if (this.lastSuitSelected == card.suit) {
				  return true;
			  }
			  else {
				  return false;
			  }
		  }
		  if (card.suit == lastCard.suit) {
			  return true;
		  }
		  if (card.number == lastCard.number) {
			  return true;
		  }
		  return false;
	  },
	  
	  openWildSelection: function() {
		  this.wildwin = new Ext.Window({
              title:'Select a Suit',
              closable: false
              ,width       : 700
              ,height      : 200
              ,layout:'table'
            	  ,layoutConfig: {
            	        // The total column count must be specified here
            	        columns: 4
            	    }
              ,items: [
                       {
                    	   html: '<img src="images/suits/club.png"></img>'
                       },
                       {
                    	   html: '<img src="images/suits/diamond.png"></img>'
                       },
                       {
                    	   html: '<img src="images/suits/spade.png"></img>'
                       },
                       {
                    	   html: '<img src="images/suits/heart.png"></img>'
                       },
                       new Ext.Button({
                    	   id: 'clubButton',
                    	   text: 'Select',
                    	   listeners: {
                    		   click: this.selectSuit
                    	   }
                       }),
                       new Ext.Button({
                    	   id: 'diamondButton',
                    	   text: 'Select',
                    	   listeners: {
                    		   click: this.selectSuit
                    	   }
                    	   
                       }),
                       new Ext.Button({
                    	   id: 'spadeButton',
                    	   text: 'Select',
                    	   listeners: {
                    		   click: this.selectSuit
                    	   }
                    	  
                       }),
                       new Ext.Button({
                    	   id: 'heartButton',
                    	   text: 'Select',
                    	   listeners: {
                    		   click: this.selectSuit
                    	   }
                    	   
                       })
                       ] 
          });
          this.wildwin.show();
	  },
	  
	  selectSuit: function(button, e) {
		  game.wildwin.close();
		  
		  var wildSuit = null;
		  if (button.id == 'clubButton') {
			  wildSuit = 'Clubs';
		  }
		  else if (button.id == 'diamondButton') {
			  wildSuit = 'Diamonds';
		  }
		  else if (button.id == 'spadeButton') {
			  wildSuit = 'Spades';
		  }
		  else if (button.id == 'heartButton') {
			  wildSuit = 'Hearts';
		  }
		  
		  var s = {};
		  s.from = 'hand';
		  s.card = game.discardpile.getLastCard().toObject();
		  s.suit = wildSuit;
		  util.discard(s);
		  
		  util.endTurn();
	  },
	  
	  discardedCard: function(details) {
		  var turnIndex = util.getTurnIndex();
		  
		  var hand = this.hands[turnIndex];
		  	  
		  var card = hand.findCard(details.card);
		  
		  card.move(this.discardpile);
		  
		  card.reveal();
		  
		  if (card.joker && card.joker == true) {
			  this.lastSuitSelected = details.suit;
		  }
	  }

});